/**
 * Created by XQ on 2017/5/31.
 */

app.controller("homePageCtrl",function($scope,$http,userSession){

    $(document).ready(function(){
        //页面js
        $(".ct_name .ct_localIcon").click(function () {
            $(".getCtName").css("display","block");
        })
        $(".cancel").click(function () {
            $(".getCtName").css("display","none");
        })

        //删除已完成行程
        var didmove = false;
        var startX,endX,moveX;
        $(".trip").on("touchstart touchend touchmove",function (e) {
                switch (e.type){
                    case "touchstart":startX = e.originalEvent.touches[0].clientX;
                        break;
                    case "touchmove":endX = e.originalEvent.touches[0].clientX;
                        $(this).css("left" ,$(this).position().left+2+"px");
                        didmove = true;
                        break;
                    case "touchend":if (didmove == false) return;didmove = false;moveX = endX - startX;
                        if (moveX > 150) {
                            $(this).detach();
                        }else {
                            $(this).css("left","0px");
                        }
                        break;
                }
            }
        )

        //点击发布按钮
        $(".my_record").click(function () {
            $(".record").css("display","block");
        })

    });

    $scope.weaInfo = {
        ctHeader:null,
        ctDate:null,
        ctTime:null,
        statesImg:null,
        degree:null,
        detail:null,
        warnInfo:null,
        warnVal:null,
        sport:null,
        clothes:null
    }
    // 得到信息方法
    function getWea(cityName) {
        $http.get("http://localhost:8080/weather/"+$scope.cityName).then(function (resp) {
            console.log(resp.data)
            $scope.weaInfo.ctHeader = resp.data.weatherInfo.ctHeader;
            $scope.weaInfo.ctDate = resp.data.weatherInfo.ctDate;
            $scope.weaInfo.ctTime = resp.data.weatherInfo.ctTime;
            $scope.weaInfo.statesImg = resp.data.weatherInfo.statesImg;
            $scope.weaInfo.degree = resp.data.weatherInfo.degree;
            $scope.weaInfo.detail = resp.data.weatherInfo.detail;
            $scope.weaInfo.warnInfo = resp.data.weatherInfo.warnInfo;
            $scope.weaInfo.warnVal = resp.data.weatherInfo.warnVal;
            $scope.weaInfo.sport = resp.data.weatherInfo.sport;
            $scope.weaInfo.clothes = resp.data.weatherInfo.clothes;
        })
    }
    $scope.cityName = "longquanyi";

    // 进入页面直接获取信息
    getWea();

    // 切换城市获取天气信息
    $scope.changeCity = function () {
        $(".getCtName").css("display","none");
        getWea($scope.cityName)

    }

    //获得今日行程
    $http.get("http://localhost:8080/notes/"+userSession.getAccount()).then(function (resp) {
        if (resp.data.length == 0){
            $(".homePage_schedule").append("<div class='trip trip_top'> 无行程 </div>");
        }else {
            $scope.record = resp.data
        }

    })
})
app.controller("myNoteCtrl",function ($scope) {

})
app.controller("personalCenterCtrl",function ($scope) {
    $(document).ready(function(){
        //点击发布按钮
        $(".my_record").click(function () {
            $(".record").css("display","block");
        })
    });
})
app.controller("consumingCenterCtrl",function ($scope,$http,userSession) {
    $scope.user = userSession.getAccount();
    $http.get("http://localhost:8080/expenseLists/18302860548").then(
        function (success) {
            console.log(success.data)
            $scope.consuming = success.data;
        },function (fail) {

        }
    )
})
app.controller("logInCtrl",function ($scope,$http,$location,userSession) {
    $scope.userLogInInfo = {
        userAccount:null,
        password:null
    }
    $scope.logIn = function () {
        if ($scope.userLogInInfo.userAccount!= null){
            $http({
                method:"post",
                url:"http://localhost:8080/login",
                data:$scope.userLogInInfo
            }).success(function (resp) {
                if (resp.meta.code == 1){
                    $location.path("/homePage")
                    userSession.setAccount($scope.userLogInInfo.userAccount)
                }else {
                    console.log(resp.meta.message)
                }
            })
        }else {
            console.log("用户密码或账户不能为空！")
        }

    }
})
app.controller("registerCtrl",function ($scope,$http,$location) {
    $scope.registerUserInfo = {
        userAccount:null,
        password:null,
        nickname:null
    }
    $scope.register = function () {
        console.log($scope.registerUserInfo)
        $http({
            method:"post",
            url:"http://localhost:8080/users",
            data:$scope.registerUserInfo
        }).success(function (resp) {
            if (resp.meta.code == 1){
                $location.path("/login")
            }else {
                console.log(resp.meta.message)
            }
        })
    }

})
app.controller("addRecordCtrl",function ($scope,$http,$location,userSession) {
    $scope.record = {
        title:null,
        userAccount:userSession.getAccount(),
        content:null,
        time:null
    }

    $scope.addRecord = function () {
        $http({
            method:"post",
            url:"http://localhost:8080/notes",
            data:$scope.record
        }).success(function (resp) {
            if (resp.meta.code == 1){
                $location.path("/homePage");
            }
        })
    }

})
app.controller("addBillCtrl",function ($scope,$http,$location,userSession) {
    $scope.bill = {
        itemName:null,
        consumption:null,
        state:null,
        time:null,
        userAccount:userSession.getAccount()
    }
    $scope.addBill = function () {
        $http({
            method:"post",
            url:"http://localhost:8080/expenseLists",
            data:$scope.bill
        }).success(function (resp) {
            $location.path("/consumingCenter")
        })
    }

})
app.controller("personalDataCtrl",function ($scope,$http,userSession) {
    $scope.userInfo = {
        headPortrait:null,
        userAccount:null,
        nickname:null,
        sex:null
    }
    $http.get("http://localhost:8080/users/"+userSession.getAccount()).then(function (resp) {
        console.log(resp)
        $scope.userInfo.userAccount = resp.data.userAccount;
        $scope.userInfo.nickname = resp.data.nickname;
        $scope.userInfo.sex = resp.data.sex;
    },function () {
        console.log("0")
    })
})
